#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <signal.h>
#include <QtDebug>
#include <QIcon>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    // 初始化账号密码
    userName = "";
    passWord = "";

    ui->setupUi(this);

    QWidget *centralWidget = new QWidget(this);
    this->setCentralWidget(centralWidget);

    QVBoxLayout *vLayout = new QVBoxLayout(centralWidget);
    vLayout->addStretch(1);

    this->setStyleSheet("MainWindow{background-color: rgb(0, 85, 255);}");

    QHBoxLayout *hLayout = new QHBoxLayout();
    vLayout->addLayout(hLayout);

    hLayout->addStretch(1);

    QWidget *loginForm = new QWidget();
    QVBoxLayout *loginForm_VLayout = new QVBoxLayout(loginForm);
    loginForm_VLayout->addStretch(1);

    // 显示标签
    QLabel *lb = new QLabel(loginForm);

    lb->setFixedWidth(500);
    lb->setFixedHeight(80);
    lb->setText("图书管理系统");
    lb->setAlignment(Qt::AlignCenter);
    lb->setStyleSheet("font-size: 48px; color: rgb(255, 0, 0);");
    loginForm_VLayout->addWidget(lb);

    loginForm_VLayout->addSpacing(50);

    // 显示账号
    QLineEdit *account = new QLineEdit(loginForm);

    account->setFixedWidth(300);
    account->setFixedHeight(45);
    account->setMaxLength(12);
    account->setStyleSheet("font-size: 24px;");
    account->setPlaceholderText("请输入账号");
    loginForm_VLayout->addWidget(account, 0, Qt::AlignHCenter);
    connect(account, SIGNAL(textChanged(const QString &)),
            this, SLOT(setUserName(const QString &)));

    loginForm_VLayout->addSpacing(20);

    // 显示密码
    QLineEdit *password = new QLineEdit(loginForm);

    password->setFixedWidth(300);
    password->setFixedHeight(45);
    password->setMaxLength(12);
    password->setEchoMode(QLineEdit::Password);
    password->setStyleSheet("font-size: 24px;");
    loginForm_VLayout->addWidget(password, 0, Qt::AlignHCenter);
    connect(password, SIGNAL(textChanged(const QString &)),
            this, SLOT(setPassWord(const QString &)));

    loginForm_VLayout->addSpacing(30);

    // 显示按钮
    QWidget *btnForm = new QWidget();

    QHBoxLayout *btnHLayout = new QHBoxLayout(btnForm);
    btnHLayout->addStretch(1);

    QIcon loginIcon(":/img/bbb.png");
    QPushButton *login_btn = new QPushButton(loginIcon, "登录", btnForm);

    login_btn->setIconSize(QSize(30, 30));
    login_btn->setObjectName("login_btn");
    login_btn->setFixedWidth(100);
    login_btn->setFixedHeight(60);
    login_btn->setStyleSheet("#login_btn{background-color:rgb(255,255,255); font-size: 32px; border-radius: 15px;} QPushButton:pressed#login_btn{background-color:rgb(143,143,143);}");
    btnHLayout->addWidget(login_btn);
    connect(login_btn, SIGNAL(clicked()), this, SLOT(login())); // 手动连接信号槽

    btnHLayout->addStretch(1);

    QPushButton *register_btn = new QPushButton("注册", btnForm);
    register_btn->setObjectName("register_btn");
    register_btn->setFixedWidth(100);
    register_btn->setFixedHeight(60);
    register_btn->setStyleSheet("#register_btn{background-color:rgb(255,255,255); font-size: 32px; border-radius: 15px;} QPushButton:pressed#register_btn{background-color:rgb(143,143,143);}");
    btnHLayout->addWidget(register_btn);
    connect(register_btn, SIGNAL(pressed()), this, SLOT(on_ccBtn_pressed()));   // 手动连接信号槽
    connect(register_btn, SIGNAL(released()), this, SLOT(on_ccBtn_released())); // 手动连接信号槽

    btnHLayout->addStretch(1);

    loginForm_VLayout->addWidget(btnForm);

    loginForm_VLayout->addStretch(1);

    hLayout->addWidget(loginForm);

    hLayout->addStretch(1);

    vLayout->addStretch(1);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeColor()
{
}

void MainWindow::on_ccBtn_pressed()
{
    this->setStyleSheet("MainWindow{background-color: rgb(255,0,0)}");
}

void MainWindow::on_ccBtn_released()
{
    this->setStyleSheet("MainWindow{background-color: rgb(0, 85, 255)}");
}

void MainWindow::setUserName(const QString &userne)
{
    userName = userne;
}

void MainWindow::setPassWord(const QString &passwd)
{
    passWord = passwd;
}

void MainWindow::login()
{
    if (userName == "gec")
    {
        if (passWord == "123456")
        {
            qDebug() << "登录成功";
        }
        else
        {
            qDebug() << "密码错误";
        }
    }
    else
    {
        qDebug() << "账号错误";
    }
}
